<?php

namespace App\Console\Commands;

use App\Article;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Symfony\Component\DomCrawler\Crawler;

class FetchNewsYyets extends Command
{
    const Base_Url = 'http://www.zimuzu.tv';
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'spider:yyetsnews';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '抓取YYets 新闻';
    protected $client;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        set_time_limit(1000);
        $base_url = 'www.zimuzu.tv';
        $header_params = [
            'Host' => $base_url,
            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Encoding' => 'gzip, deflate, sdch',
            'Accept-Language' => 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
            'Cache-control:' => 'max-age=0',
            'Dnt' => '1',
            'Referer' => 'https://www.baidu.com/link?url=OOIVn0qbApPkoe99TFbeGI307BTpCTY4pXGJcll69PEQRzlZjqbxMmnq9DoI3AWQ&wd=&eqid=fe299461002fe7600000000557314a9e',
            'Upgrade-Insecure-Requests' => '1',
            'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
        ];
        //获取登陆cookie
        $this->client = new Client([
            'headers' => $header_params,
            'cookies' => true,
            'allow_redirects' => true,
            'base_uri' => $base_url,
            'http_errors' => false,
        ]);
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //http://www.zmz2017.com/search/index?keyword=game+of+throne&search_type=
        $url = 'http://www.zimuzu.tv/article';

        $res = $this->client->request('GET', $url);

        if ($res->getStatusCode() != 200) {
            \Log::critical('抓取失败fetch news yyeets  index' . $url);
            return;
        }

        //$res = $client->request('GET', '/article',['query' => ['page' => 10]]);
        $dom = $res->getBody()->getContents();
        $crawler = new Crawler($dom);

        $crawler->filter('div.article-list > ul > li > h3 > a')->each(function (Crawler $aNode, $i) {
            $base_url = self::Base_Url;
            $url = $aNode->attr('href');
            $url_provider = $base_url . $url;
            $page = $this->client->request('GET', $url_provider);

            if ($page->getStatusCode() == 200) {
                try {
                    $dom = $page->getBody()->getContents();
                    $page_html = new Crawler($dom);
                    $body = $page_html->filter('div.information-box.box > div.information-con > div.information-desc')->first()->html();
                    $title = $page_html->filter('body > div.middle-box > div > div.information-box.box > h2')->first()->text();
                    $imgUrls = [];
                    $page_html->filter('div.information-box.box img')->each(function ($node, $idx) use (&$imgUrls) {
                        $imgUrls[] = $node->attr('src');
                    });
                    Article::Spider_Insert($title, $body, $url_provider, $imgUrls);
                } catch (\Exception $e) {
                    $mesage = '爬去美剧天涯失败:' . $url_provider . "\n" . $e->getMessage() . "\n";
                    \Log::alert($mesage);
                    echo $mesage;
                }
            }

        });

    }
}
